﻿using Common.Device.Enums;
using Common.Device.Models;
using Common.Tool.DataTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// DianZiFuZaiView.xaml 的交互逻辑
    /// </summary>
    public partial class DianZiFuZaiView : UserControl
    {
        private ViewModels.DianZiFuZaiViewModel viewModel = new ViewModels.DianZiFuZaiViewModel();
        public DianZiFuZaiView(DeviceEnum.DeviceModelEnum model, Order order = null)
        {
            InitializeComponent();
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out DianZiFuZaiEnum.TestMode mode);
                DianZiFuZai parameter = JsonConvert.DeserializeObject<DianZiFuZai>(order.deviceModel);
                switch (mode)
                {
                    case DianZiFuZaiEnum.TestMode.设置负载:
                        viewModel.MenuIndex = 0;
                        viewModel.SetMode = parameter.负载模式;
                        viewModel.SetValue = parameter.消耗值.ToString();
                        break;
                    case DianZiFuZaiEnum.TestMode.读取设置负载:
                        viewModel.MenuIndex = 1;
                        viewModel.ReadSetMode = parameter.负载模式;
                        viewModel.ReadSetMin = parameter.下限值?.ToString();
                        viewModel.ReadSetMax = parameter.上限值?.ToString();
                        break;
                    case DianZiFuZaiEnum.TestMode.读取当前负载:
                        viewModel.MenuIndex = 2;
                        viewModel.ReadCurrentMode = parameter.负载模式;
                        viewModel.ReadCurrentMin = parameter.下限值?.ToString();
                        viewModel.ReadCurrentMax = parameter.上限值?.ToString();
                        break;
                    case DianZiFuZaiEnum.TestMode.关闭负载:
                        viewModel.MenuIndex = 3;
                        break;
                    default:
                        break;
                }
            }
        }
        //仪器对象
        public Interfaces.IDianZiFuZai Device;
        private void SetClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? value = null;
            if (viewModel.SetValue.Length == 1 && char.IsLetter(viewModel.SetValue[0]))
            {
                if (viewModel.SetValue[0] < 'a' || viewModel.SetValue[0] > 'z')
                {
                    View.Controls.Dialogs.TipView.Show("消耗值输入有误!");
                    return;
                }
            }
            else if (!DataConvert.GetDouble(viewModel.SetValue, ref value) || value == null)
            {
                View.Controls.Dialogs.TipView.Show("消耗值输入有误!");
                return;
            }
            //创建参数
            DianZiFuZai parameter = new DianZiFuZai()
            {
                mode = DianZiFuZaiEnum.TestMode.设置负载,
                负载模式 = viewModel.SetMode,
                消耗值 = viewModel.SetValue,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void ReadSetClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.ReadSetMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.ReadSetMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            DianZiFuZai parameter = new DianZiFuZai()
            {
                mode = DianZiFuZaiEnum.TestMode.读取设置负载,
                负载模式 = viewModel.ReadSetMode,
                上限值 = max,
                下限值 = min,
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void ReadCurrentClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.ReadCurrentMax, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.ReadCurrentMin, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            DianZiFuZai parameter = new DianZiFuZai()
            {
                mode = DianZiFuZaiEnum.TestMode.读取当前负载,
                负载模式 = viewModel.ReadCurrentMode,
                上限值 = max,
                下限值 = min,
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void CloseClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            DianZiFuZai parameter = new DianZiFuZai()
            {
                mode = DianZiFuZaiEnum.TestMode.关闭负载
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }
    }
}
